<?
//Languages 0.0.3
class sym_languages extends symbiont{
    public function ___main($design=''){
        global $sql, $kernel;
        $design=$this->_check($design, 'main');
        $d=new design($design, 'languages', false);
        $this->languages='';
        $languages=$sql->select_each('languages', array('abbr', 'title'), array('is_enabled'=>1), '', 'position');
        foreach($languages as $val){
            $this->abbr=$val['abbr'];
            $this->title=$val['title'];
            if($val['abbr']==$kernel->lang->abbr) $this->current=true;
            else $this->current=false;
            $this->languages.=design::run($this->language);
        }
        $d->inc();
    }
    public function ___admin($design=''){
        global $sql;
        $design=$this->_check($design, 'admin');
        $d=new design($design, 'languages', false);
        $this->languages='';
        $languages=$sql->select_each('languages', '*', '', '', 'position');
        foreach($languages as $val){
            $this->id=$val['id'];
            $this->abbr=$val['abbr'];
            $this->title=$val['title'];
            $this->title_en=$val['title_en'];
            $this->enabled=$val['is_enabled']?'checked="checked"':'';
            $this->def=$val['is_default']?'checked="checked"':'';
            $this->languages.=design::run($this->language);
        }
        $d->inc();
    }
    public function ___add($design=''){
        $design=$this->_check($design, 'add');
        $this->id=$this->abbr=$this->title=$this->title_en='';
        $d=new design($design, 'languages', true);
    }
    public function ___edit($design='', $id){
        global $sql;
        $design=$this->_check($design, 'add');
        $id=data::number($id);
        $language=$sql->select_each('languages', '*', array('id'=>$id), '', '', 1);
        $this->id=$id;
        $this->abbr=$language['abbr'];
        $this->title=$language['title'];
        $this->title_en=$language['title_en'];
        $d=new design($design, 'languages', true);
    }
    public function ___delete($design=''){
        $design=$this->_check($design, 'delete');
        $d=new design($design, 'languages', true);
    }
    
    //DB functions
    //Language adding
    public function ___db_add($design=''){
        global $user, $sql;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['abbr'])||!isset($_POST['title'])||!isset($_POST['title_en'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $values['abbr']=data::word($_POST['abbr'], 'en', 2);
        $values['title']=data::html_remove($_POST['title']);
        $values['title_en']=data::html_remove($_POST['title_en']);
        $sql->insert('languages', $values);
        print('{"ok":"Language added."}');
    }
    //Language editiong
    public function ___db_edit($design=''){
        global $user, $sql;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['id'])||!isset($_POST['abbr'])||!isset($_POST['title'])||!isset($_POST['title_en'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $where['id']=data::number($_POST['id']);
        $values['abbr']=data::word($_POST['abbr'], 'en', 2);
        $values['title']=data::html_remove($_POST['title']);
        $values['title_en']=data::html_remove($_POST['title_en']);
        $sql->update('languages', $values, $where, 1);
        print('{"ok":"Language edited."}');
    }
    //Language deleting
    public function ___db_delete($design=''){
        global $user, $sql;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $where['id']=data::number($_POST['id']);
        $sql->delete('languages', $where, 1);
        print('{"ok":"Language deleted."}');
    }
    //Set language as default
    public function ___db_default($design=''){
        global $user, $sql;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['id'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $id=data::number($_POST['id']);
        $sql->update('languages', array('is_default'=>0));
        $sql->update('languages', array('is_default'=>1), array('id'=>$id));
        print('{"ok":"Defautl language is changed."}');
    }
    //Turn off or turn on language
    public function ___db_enabled($design=''){
        global $user, $sql;
        if($user->access_level<9){
            print('{"error":"You have not enought prerogatives."}');
            return;
        }
        if(!isset($_POST['id'])||!isset($_POST['enabled'])){
            print('{"error":"Not the all parametrs have been sent."}');
            return;
        }
        $id=data::number($_POST['id']);
        $enabled=data::number($_POST['enabled']);
        $sql->update('languages', array('is_enabled'=>$enabled), array('id'=>$id));
        print('{"ok":'.($enabled==1?'"Lanuage is on"':'"Language is off"').'}');
    }
    //Sort language
    public function ___db_sort($design=''){
        global $user, $sql;
        if($user->access_level<9) return;
        foreach($_POST['sort'] as $key=>$val){
            $id=data::number($val);
            $position=data::number($key)+1;
            $sql->update('languages', array('position'=>$position), array('id'=>$id), 1);
        }
    }
}
?>